package org.qydjk.console.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.qydjk.common.dao.impl.BaseDao;
import org.qydjk.common.pojo.user.User;
import org.qydjk.common.pojo.user.UserMoneyDetail;
import org.qydjk.common.pojo.user.UserMoneyExtractDetail;
import org.qydjk.common.pojo.user.UserMoneyExtractRecord;
import org.qydjk.common.shiro.vo.ShiroUser;
import org.qydjk.console.service.IUserWithdrawalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by Administrator on 2018/1/11 0011.
 */
@Service
public class UserWithdrawalsService implements IUserWithdrawalsService {
    @Autowired
    private BaseDao baseDao;

    public Object updateUserIthdrawlsNO(Integer id, String remark, ShiroUser shiroUser) throws Exception {
        Date date = new Date();

        UserMoneyExtractRecord userMoneyExtractRecord = baseDao.get(UserMoneyExtractRecord.class, id.intValue());
        if (userMoneyExtractRecord.getType() == 2) {
            return fail("该提现已通过,无法拒绝!");
        } else if (userMoneyExtractRecord.getType() == 3) {
            return fail("已完成提现, 操作失败");
        } else if (userMoneyExtractRecord.getType() == 1) {
            return fail("不能重复操作!");
        }

        // 修改提现申请记录表
        String typeName = "拒绝提现";
        Integer type = 1;
        updateMoneyExtract(userMoneyExtractRecord, type, typeName, date);

        // 修改相关余额提现明细表
        addMoneyExtractDetail(id, remark, type, typeName, shiroUser, date);

        // 获取用户信息
        Integer userId = userMoneyExtractRecord.getUserId();
        User user = baseDao.get(User.class, userId);
        // 余额明细加入 记录
        UserMoneyDetail userMoneyDetail = new UserMoneyDetail();
        userMoneyDetail.setUserId(userId);
        userMoneyDetail.setPreMoney(user.getMoney());
        userMoneyDetail.setOpMoney(userMoneyExtractRecord.getExtractMoney());
        userMoneyDetail.setOpTime(date);
        userMoneyDetail.setOpType(3);
        userMoneyDetail.setOpDes("提现拒绝");
        userMoneyDetail.setUpdateTime(date);
        userMoneyDetail.setCreateTime(date);

        // 操作金额退回账户余额
        user.setMoney(user.getMoney().add(userMoneyExtractRecord.getExtractMoney()));
        user.setUpdateTime(date);
        baseDao.update(user);

        userMoneyDetail.setAftMoney(user.getMoney());
        baseDao.save(userMoneyDetail);

        return success();
    }

    public Object updateUserIthdrawlsYes(Integer id, ShiroUser shiroUser) throws Exception {
        Date date = new Date();

        UserMoneyExtractRecord userMoneyExtractRecord = baseDao.get(UserMoneyExtractRecord.class, id.intValue());
        if (userMoneyExtractRecord.getType() == 1) {
            return fail("已拒绝提现, 操作失败");
        } else if (userMoneyExtractRecord.getType() == 2) {
            return fail("不能重复操作!");
        } else if (userMoneyExtractRecord.getType() == 3) {
            return fail("已完成提现, 操作失败");
        }

        // 修改提现申请记录表
        String typeName = "提现通过";
        Integer type = 2;
        updateMoneyExtract(userMoneyExtractRecord, type, typeName, date);
        // 修改相关余额提现明细表
        addMoneyExtractDetail(id, "该提现申请, 经审核无问题, 同意提现, 请尽快处理", type, typeName, shiroUser, date);
        return success();
    }

    public Object updateUserIthdrawlsFinash(Integer id, ShiroUser shiroUser) throws Exception {
        Date date = new Date();

        UserMoneyExtractRecord userMoneyExtractRecord = baseDao.get(UserMoneyExtractRecord.class, id.intValue());
        if (userMoneyExtractRecord.getType() == 0) {
            return fail("该提现申请尚未通过, 操作失败");
        } else if (userMoneyExtractRecord.getType() == 1) {
            return fail("已拒绝提现, 操作失败");
        } else if (userMoneyExtractRecord.getType() == 3) {
            return fail("不能重复操作!");
        }

        // 修改提现申请记录表
        String typeName = "提现完成";
        Integer type = 3;
        updateMoneyExtract(userMoneyExtractRecord, type, typeName, date);
        // 修改相关余额提现明细表
        addMoneyExtractDetail(id, "该提现申请, 经审核无问题, 已完成提现", type, typeName, shiroUser, date);
        return success();
    }

    private void updateMoneyExtract (UserMoneyExtractRecord userMoneyExtractRecord, Integer type, String typeName, Date date) throws Exception {
        userMoneyExtractRecord.setType(type);
        userMoneyExtractRecord.setTypeName(typeName);
        userMoneyExtractRecord.setUpdateTime(date);
        baseDao.update(userMoneyExtractRecord);
    }

    private void addMoneyExtractDetail (Integer id, String remark, Integer type, String typeName, ShiroUser shiroUser, Date date) throws Exception {
        UserMoneyExtractDetail userMoneyExtractDetail = new UserMoneyExtractDetail();
        userMoneyExtractDetail.setExtractRecordId(id);
        userMoneyExtractDetail.setType(type);
        userMoneyExtractDetail.setTypeName(typeName);
        userMoneyExtractDetail.setRemark(remark);
        userMoneyExtractDetail.setOpUserId(shiroUser.getId());
        userMoneyExtractDetail.setOpUsername(shiroUser.getLoginName());
        userMoneyExtractDetail.setUpdateTime(date);
        userMoneyExtractDetail.setCreateTime(date);
        baseDao.save(userMoneyExtractDetail);
    }

    private Object success() {
        JSONObject resp = new JSONObject();
        resp.put("success", true);
        resp.put("msg", "操作成功!");
        return JSON.toJSON(resp);
    }

    private Object fail(String mag) {
        JSONObject resp = new JSONObject();
        resp.put("success", false);
        resp.put("msg", mag);
        return JSON.toJSON(resp);
    }
}
